home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
vmed.arc
/
VMIO.CCC
< prev
next >
Wrap
Text File
|
1985-12-03
|
4KB
|
146 lines
/* VMIO/CCC
* virtual memory add/move/delete/insert
*
* Copyright 1983 by Jim Kyle - All Rights Reserved
* Licensed for individual non-commercial use only.
*
* created: November 16, 1983 - Jim Kyle
* last changed: November 30, 1983 - Jim Kyle
*/
/*
* ins_btm() inserts a line in 'from' as the last line
* of the current block and updates all block data. If
* no room exists, a new block is added.
*/
ins_btm(from) char *from;
{ int len,sz;
t_r("ins_btm");
if ((len = getwd(from)) > BDSZ)
return(vm_err("ins_btm; bad input"));
sz = cur_size() + 1;
if ((sz + len) > BLKSIZ)
if (forcenew()) return(ERR);
return(cpy_in(from, eob(), len));
}
/*
* ins_top() inserts a line in 'from' as the first line
* of the current block and updates all block data. If
* no room exists, a new block is added.
*/
ins_top(from) char *from;
{ int len,sz;
t_r("ins_top");
if ((len = getwd(from)) > BDSZ)
return(vm_err("ins_top; bad input"));
sz = cur_size() + 1;
if ((sz + len) > BLKSIZ) {
if (go_pvb())
return(vm_err("ins_top prev"));
if (forcenew()) return(ERR);
}
else {
sz -= BHSZ;
vm_move(Dbptr,Dbptr+len,sz);
}
return(cpy_in(from,Dbptr,len));
}
/*
* ln_del() deletes the line pointed to, decrements
* the last-line counts, and smudges the block. It
* does NOT alter the current-line pointers.
*/
ln_del(p) char *p; /* delete line at *p */
{ int n;
t_r("ln_del");
n = getwd(p); /* length deleted */
vm_abt(n == 0,"ln_del, null count");
vm_move(p+n, p, eob()-p-n); /* close up data */
*Bcptr -= n; /* adjust byte count */
--Llcb; /* last line this block */
--Lltf; /* last line total file */
smudge();
--*Lcptr; /* all done */
}
mvln_nx() /* move last line to top of next blk */
{ int svcln;
char *p;
t_r("mvln_nx");
svcln = Cur_ln; /* save current line nbr */;
p = llad(); /* save lastline pointer */
if (*Nxptr) /* not at EOF */
go_nxb(); /* so step to next */
else /* at EOF */
forcenew(); /* so force another block */
ins_top(p); /* copy in the line */
go_pvb(); /* back to original block */
ln_del(p); /* delete last line */
go_to(svcln); /* and restore Cur_ln location */
}
mvln_pv() /* move top line to bottom of prev blk */
{ int svcln;
char *p;
t_r("mvln_pv");
if (Curr == 0) return; /* at front of file */
p = Dbptr; /* save firstline pointer */
svcln = Cur_ln; /* and current-line nbr */
go_pvb(); /* make prev current */
ins_btm(p); /* copy in the line */
while (Dbptr != p)
go_nxb(); /* back to original block */
ln_del(p); /* delete first line */
go_to(svcln); /* and restore pointers */
}
/*
* forcenew() forces addition of a new block after the
* current block and makes the new block current.
*/
forcenew()
{ if (add_blk()) return(vm_err("forcenew add"));
if (vm_get(*Nxptr)) return(vm_err("forcenew nxt"));
Flcb = Llcb + 1; /* Llcb does not change */
smudge();
return(OK);
}
/*
* cpy_in() copies 'len' bytes from 'src' to 'dst' and
* updates pointers for the current block, assuming that
* 'dst' is within the current block's area.
*/
cpy_in(src,dst,len) int len; char *src,*dst;
{ t_r("cpy_in entry");
*Bcptr += len; /* current block byte count */
++(*Lcptr); /* current block line count */
++Llcb; /* current block last line */
++Lltf; /* total file line count */
vm_move(src,dst,len);
smudge();
t_r("cpy_in exit");
return(OK);
}
/*
* open_hole() opens a hole in the current block
* for 'size' bytes, immediately before the current
* line. If no room exists, enough lines are moved
* to the next block to make room (inserting a new
* next block if necessary). Block pointers are
* NOT adjusted; cpy_in() will fix them.
*/
open_hole(size) int size;
{ vm_abt(size >= BDSZ, "open_hole");
t_r("open_hole entry");
while ((size + cur_size() + 1) >= BLKSIZ)
mvln_nx(); /* move enough to make hole */
vm_move(clad(),clad()+size,eob()-clad()+1);
putwd(size,clad()); /* keep size pointers OK */
t_r("open_hole exit");
}